﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

public partial class search_electronic : System.Web.UI.Page
{
    public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID, ref EnterpriseName, ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }
        this.EnableViewState = false;
        Response.Write(Init.InitIndex(EnterpriseName, UserPower));
        Footer = Init.IndexFooter();
        ddh = Request.QueryString["d"];
        ckdh = Request.QueryString["c"];
        srq = Request.QueryString["s"];
        erq = Request.QueryString["e"];
        wldw = Request.QueryString["w"];
        if (!string.IsNullOrEmpty(ddh) || !string.IsNullOrEmpty(ckdh) || !string.IsNullOrEmpty(srq) || !string.IsNullOrEmpty(erq) || !string.IsNullOrEmpty(wldw))
        {
            DataSet ds = DataGroup(ddh, ckdh, wldw, srq, erq);
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] { "djbh" };//主键
            int recordCount = ds.Tables[0].Rows.Count;
            GridView1.DataBind();
        }
    }

    private string mssqlConnect = ConfigurationManager.ConnectionStrings["DZFPmssqlConnect"].ToString();
    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public string ddh = "";
    public string ckdh = "";
    public string srq = "";
    public string erq = "";
    public string wldw = "";

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataSet ds = DataGroup(ddh, ckdh, wldw, srq, erq);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        int recordCount = ds.Tables[0].Rows.Count;
        GridView1.DataBind();
    }

    //展示
    public DataSet DataGroup(string ddh, string ckdh, string wldw = "", string startrq = "", string endrq = "")
    {
        string sql = "select top 2000 rtrim(left(a.djbh,14)) as djbh,rtrim(ddh) as ddh,FP_DM,FP_HM,KPRQ,a.KPHJJE,PDF_URL,rtrim(a.wldwname) as company,rtrim(a.wldwid) as dwid "
                          + " from DZFPHZ a(nolock),DZFPSQHZ_LP b(nolock) where replace(a.djbh,'HP-','')=b.djbh";
        if (string.IsNullOrEmpty(wldw))
        {
            sql += (UserPower.Contains("Manager")) ? "" : " and a.wldwname like '%" + EnterpriseName + "%' ";
        }
        else
        {
            sql += " and a.wldwname like '%" + wldw + "%' ";
        }
        if (!string.IsNullOrEmpty(ddh))
        {
            sql += " and a.ddh='" + ddh + "' ";
        }
        if (!string.IsNullOrEmpty(ckdh))
        {
            sql += " and left(a.djbh,14)='" + ckdh + "' ";
        }
        if (!string.IsNullOrEmpty(startrq))
        {
            sql += " and a.kprq>='" + startrq + "' ";
        }
        if (!string.IsNullOrEmpty(endrq))
        {
            sql += " and a.kprq<='" + endrq + "' ";
        }
        sql += " order by a.KPRQ desc,a.djbh desc";
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        DataSet dss = mssql.ExecuteDs(sql, mssqlConnect);
        return dss;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = DataGroup(ddh, ckdh, wldw, srq, erq);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        int recordCount = ds.Tables[0].Rows.Count;
        GridView1.DataBind();
    }

}